package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2019/7/9.
 */
@Controller
@RequestMapping("travel")
public class TravelController {
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelCommentService travelCommentService;
    @RequestMapping("")
    public String index(Model model, @ModelAttribute("qo") TravelQuery qo) {
        qo.setState(Travel.STATE_RELEASE);
        model.addAttribute("pageInfo", travelService.query(qo));
        return "travel/list";
    }

    @RequestMapping("detail")
    public String detail(Long id, Model model) {
        //detail 游记明细
        Travel travel = travelService.get(id);
        TravelContent travelContent = travelService.getContent(id);
        travel.setTravelContent(travelContent);
        model.addAttribute("detail", travel);
        //toasts 目的地吐司
        List<Destination> toasts = destinationService.getToasts(travel.getDest().getId());
        Destination dest = toasts.get(toasts.size() - 1);
        toasts.remove(dest);
        model.addAttribute("toasts", toasts);
        //sds 阅读量前三攻略明细
        model.addAttribute("sds", strategyDetailService.getDetailTop3(travel.getDest().getId()));
        //ts 阅读量前三游记
        model.addAttribute("ts", travelService.queryTop3Byview(travel.getDest().getId()));
        //list 评论列表
        TravelCommentQuery qo = new TravelCommentQuery();
        qo.setTravelId(id);
        qo.setPageSize(Integer.MAX_VALUE);
        Page<TravelComment> page = travelCommentService.query(qo);
        model.addAttribute("list",page.getContent());
        return "travel/detail";
    }

    @RequireLogin
    @RequestMapping("input")
    public String input(Long id, Model model) {
        //tv
        if (id != null) {
            Travel travel = travelService.get(id);
            TravelContent travelContent = travelService.getContent(id);
            travel.setTravelContent(travelContent);
            model.addAttribute("tv", travel);
        }
        //regions
        model.addAttribute("regions", destinationService.getDestByDeep(3));
        return "travel/input";
    }


    @Value("${file.path}")
    private String filePath;
    //上传图片
    @RequestMapping("/coverImageUpload")
    @ResponseBody
    public Object coverImageUpload(MultipartFile pic) {
        String upload = UploadUtil.upload(pic, filePath);
        return upload;
    }

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile,HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, filePath);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo){
        //将当前登录用户设置为作者
        travel.setAuthor(userInfo);
        Long travelId = travelService.saveOrUpdate(travel);
        return new AjaxResult().addData(travelId);
    }
    @RequestMapping("commentAdd")
    @RequireLogin
    public String commentAdd(Model model,int floor,TravelComment comment,@UserParam UserInfo userInfo){
        comment.setLevel(userInfo.getLevel());
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        TravelComment c = travelCommentService.save(comment);
        model.addAttribute("c",c);
        model.addAttribute("floor",floor+1);
        return "travel/commentTpl";
    }
}